package com.zzw.service.impl;

import com.zzw.dao.UserDao;
import com.zzw.pojo.User;
import com.zzw.service.UserService;
import com.zzw.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    public UserDao userDao;

    @Override
    public List<User> findAll2() {
        return userDao.findAll();
    }

    @Override
    public User findUser(User user) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String password = user.getPassword();
        String pwd = MD5Util.encoderPassword(password);
        user.setPassword(pwd);
        return userDao.findByUsernameAndPassword(user);
    }

    @Override
    public void addUser(User user) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String password = user.getPassword();
        String pwd = MD5Util.encoderPassword(password);
        user.setPassword(pwd);
        userDao.addUser(user);
    }

    @Override
    public void updateUser(User user) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String password = user.getPassword();
        String pwd = MD5Util.encoderPassword(password);
        user.setPassword(pwd);
        userDao.updatePassword(user);
    }
}
